package com.yingjia.yingjiauniversity.retrofit;

import com.yingjia.yingjiauniversity.retrofit.preference.Preference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import okhttp3.logging.HttpLoggingInterceptor;

public class HttpLogger implements HttpLoggingInterceptor.Logger {

    private static final Logger logger = LoggerFactory.getLogger(HttpLogger.class);

    private HttpLoggingInterceptor.Level level;

    public HttpLogger(HttpLoggingInterceptor.Level level) {
        this.level = level;
    }

    @Override
    public void log(String message) {
        switch (level) {
            case NONE:
                break;
            case BASIC:
                logger.info(message);
                break;
            case HEADERS:
                logger.info(message);
                break;
            case BODY:
                logger.debug(message);
                break;
        }
    }

    /**
     * 获取日志级别
     *
     * @return
     */
    public static HttpLoggingInterceptor create() {
        //设置retrofit的日志打印
        HttpLoggingInterceptor.Level level;
        String levelValue = Preference.getValue(Preference.LOG_LEVEL);
        if (levelValue == null ||
                (level = HttpLoggingInterceptor.Level.valueOf(levelValue)) == null) {
            level = HttpLoggingInterceptor.Level.BODY;
        }
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLogger(level));
        logging.setLevel(level);
        return logging;
    }
}
